package com.quanyue.haircut.shop.service.impl;

import com.quanyue.auto.util.AssertUtils;
import com.quanyue.haircut.domain.entity.UserCoupon;
import com.quanyue.haircut.shop.dto.CouponDetail;
import com.quanyue.haircut.shop.mapper.CouponUserMapper;
import com.quanyue.haircut.shop.service.CouponUserService;
import com.vhuomai.mybatis.mapper.page.Page;
import com.vhuomai.mybatis.mapper.page.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CouponUserServiceImpl implements CouponUserService {
    @Autowired
    private CouponUserMapper couponUserMapper;

    @Override
    public void createCouponUser(List <UserCoupon> userCoupons) {
        userCoupons.forEach(userCoupon -> {
            userCoupon.setCreateTime(LocalDateTime.now());
            userCoupon.setUseTime(LocalDateTime.now());
            userCoupon.setDelFlag(false);
        });
        couponUserMapper.insertAll(userCoupons);
    }

    @Override
    public void deleteCouponUser(Integer couponUserId) {
        UserCoupon check = couponUserMapper.findOne(couponUserId);
        AssertUtils.notNull(check, "用户优惠券信息校检出错");
        check.setDelFlag(true);
        check.setUseTime(LocalDateTime.now());
        couponUserMapper.updateSelective(check);
    }

    @Override
    public Page <CouponDetail> couponUser(PageRequest request, Integer bit, Integer userId) {
        request.setAsc(true);
        request.setOrderBy("expire_time");
        return couponUserMapper.findByBit(request, bit, userId);
    }
}
